package com.drinks.yiniang.ui.activity.user.wallet;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.user.DrawCashInfoJson;
import com.drinks.yiniang.io.api.UserApiIO;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.layout.TextEditLayout;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 编辑提现账号页面
 *
 * @author han
 * @date 2019/09/06
 */
public class EditDrawCashAccountActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_edit_alipay_account;
    //姓名
    private TextEditLayout tel_edit_alipay_name;
    //账号
    private TextEditLayout tel_edit_alipay_account;
    //确认
    private Button btn_edit_alipay_sure;

    @Override
    protected void initView() {
        nav_bar_edit_alipay_account = findViewById(R.id.nav_bar_edit_alipay_account);

        tel_edit_alipay_name = findViewById(R.id.tel_edit_alipay_name);
        tel_edit_alipay_account = findViewById(R.id.tel_edit_alipay_account);

        btn_edit_alipay_sure = findViewById(R.id.btn_edit_alipay_sure);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        getDrawCashInfo();
    }

    @Override
    protected void initListener() {
        nav_bar_edit_alipay_account.setOnNavgationBarClickListener(this);
        btn_edit_alipay_sure.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_edit_alipay_sure://确认修改
                setAlipayAccount();
                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_edit_draw_cash_account;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    //设置支付宝账号
    private void setAlipayAccount() {
        String name = tel_edit_alipay_name.getEditText();

        if (StringUtils.isEmpty(name)) {
            ToastUtils.showShort(R.string.text_hint_input_name);
            return;
        }

        String account = tel_edit_alipay_account.getEditText();

        if (StringUtils.isEmpty(account)) {
            ToastUtils.showShort(R.string.text_alipay_account);
            return;
        }

        showLoadingDialog();

        UserApiIO.getInstance().setAlipayAccount(name, account, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    //获取提现相关信息
    private void getDrawCashInfo() {
        showLoadingDialog();

        UserApiIO.getInstance().getDrawCashInfo(new APIRequestCallback<DrawCashInfoJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(DrawCashInfoJson res) {
                tel_edit_alipay_name.setEditText(res.getData().getAlipay_name());
                tel_edit_alipay_account.setEditText(res.getData().getAlipay_account());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }
}
